package com.longdaji.decoration.ui.detail;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.GoodsCommentBean;
import com.longdaji.decoration.data.bean.GoodsDetailImageBean;
import com.longdaji.decoration.data.bean.GoodsImagesBean;
import com.longdaji.decoration.data.bean.GoodsSkuBean;
import com.longdaji.decoration.data.bean.ShareUrlBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.RxUtil;
import com.longdaji.decoration.utils.TimeUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class GoodsDetailPresenter extends RxPresenter<GoodsDetailContract.View> implements GoodsDetailContract.Presenter {
    private final static String TAG = GoodsDetailPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public GoodsDetailPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void getBannerImagesAndVideos(String goodId) {
        addSubscribe(mDataManager.getGoodsImages(goodId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsImagesBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsImagesBean>() {
                    @Override
                    public void onSuccess(GoodsImagesBean goodsImagesBean) {
                        if (mView != null) {
                            mView.showBanner(goodsImagesBean.getGoodsImgs());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void getGoodsDetail(String goodId, String userId) {
        addSubscribe(mDataManager.getGoodsDetail(goodId, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsSkuBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsSkuBean>() {
                    @Override
                    public void onSuccess(GoodsSkuBean goodsSkuBean) {
                        Log.d(TAG, "GoodsSkuBean is" + goodsSkuBean.toString());
                        if (mView != null) {
                            Log.d(TAG, "getGoodsDetail is" + goodsSkuBean.getGoods());
                            mView.showGoodsDetail(goodsSkuBean.getGoods());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void getGoodsDetailImage(String goodId) {
        addSubscribe(mDataManager.getGoodsDetailImage(goodId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsDetailImageBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsDetailImageBean>() {
                    @Override
                    public void onSuccess(GoodsDetailImageBean goodsDetailImageBean) {
                        if (mView != null) {
                            Log.d(TAG, "GoodsDetailImageBean is" + goodsDetailImageBean);
                            if (goodsDetailImageBean != null && !goodsDetailImageBean.getGoodsDetailImgs().isEmpty()) {
                                mView.showGoodsDetailImage(goodsDetailImageBean.getGoodsDetailImgs().get(0).getImgAddress());
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void getGoodsComments(String goodId, String userId) {
//        addSubscribe(mDataManager.getGoodsComments(goodId, userId)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtil.<GoodsCommentListBean>handleResult())
//                .subscribeWith(new CommonSubscriber<GoodsCommentListBean>() {
//                    @Override
//                    public void onSuccess(GoodsCommentListBean goodsCommentListBean) {
//
//                        if (mView != null) {
//                            Log.d(TAG, "GoodsCommentListBean is" + goodsCommentListBean);
//                            mView.showDefaultComment(goodsCommentListBean);
//                        }
//                    }
//
//                    @Override
//                    public void onFailed(String msg) {
//                        Log.d(TAG, "msg is " + msg);
//                        if (mView != null) {
//                            mView.toast("获取评论信息失败");
//                        }
//                    }
//                }));
    }

    @Override
    public void getDefaultComment(String goodId) {
        addSubscribe(mDataManager.getDefaultComment(goodId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsCommentBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsCommentBean>() {
                    @Override
                    public void onSuccess(GoodsCommentBean goodsCommentBean) {

                        if (mView != null) {
                            Log.d(TAG, "GoodsCommentBean is" + goodsCommentBean);
                            mView.showDefaultComment(goodsCommentBean);
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            //mView.toast("获取评论信息失败");
                        }
                    }
                }));
    }

    @Override
    public void addGoodsSkuToCart(GoodsSkuBean.Goods.GoodsSku sku, int quantity) {
        Map<String, Object> options = new HashMap<>();
        options.put("goodsId", sku.getGoodsId());
        options.put("skuId", sku.getSkuId());
        options.put("userId", Account.getInstance().getUserid());
        options.put("goodsSkuNum", quantity + "");
        //options.put("goodsSkuImg", sku.getGoodsSkuImg());
        options.put("goodsSkuPrice", sku.getPrice() + "");

        addSubscribe(mDataManager.addGoodsSkuToCart(options)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {

                        if (mView != null) {
                            Log.d(TAG, "httpResponse is " + httpResponse);
                            if ("0".equals(httpResponse.getStatusCode())) {
                                mView.addToCartSuccess();
                            } else {
                                mView.addToCartFail(httpResponse.getMessage());
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.addToCartFail(msg);
                        }
                    }
                }));
    }

    @Override
    public void likeOrDislikeGoods(String userId, String commentId, final String isLike) {
        addSubscribe(mDataManager.likeGoodsOrNot(userId, commentId, isLike)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {

                        if (mView != null) {
                            Log.d(TAG, "likeOrDislikeGoods is" + httpResponse);
                            if ("1".equals(isLike)) {
                                if ("0".equals(httpResponse.getStatusCode())) {
                                    mView.showLikeSuccess();
                                } else {
                                    mView.showLikeFail();
                                }
                            } else {
                                if ("0".equals(httpResponse.getStatusCode())) {
                                    mView.showDisLikeSuccess();
                                } else {
                                    mView.showDisLikeFail();
                                }
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void collectCurGoods(String userId, String goodId) {
        List<String> idList = new ArrayList<>();
        idList.add(goodId);
        // Log.d(TAG, "collectChartGoods idList=" + idList.toString());
        addSubscribe(mDataManager.collectGoodsList(userId, idList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        if (mView != null) {
                            Log.d(TAG, "collectChartGoods" + httpResponse);
                            if ("0".equals(httpResponse.getStatusCode())) {
                                mView.showCollectSuccess();
                            } else {
                                mView.showCollectFail();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void disCollectCurGoods(String userId, String goodId) {
        List<String> idList = new ArrayList<>();
        idList.add(goodId);
        // Log.d(TAG, "collectChartGoods idList=" + idList.toString());
        addSubscribe(mDataManager.disCollectGoodsList(userId, idList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        if (mView != null) {
                            Log.d(TAG, "collectChartGoods" + httpResponse);
                            if ("0".equals(httpResponse.getStatusCode())) {
                                mView.showDisCollectSuccess();
                            } else {
                                mView.showDisCollectFail();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void getShareUrl() {
        addSubscribe(mDataManager.getShareUrl()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<ShareUrlBean>handleResult())
                .subscribeWith(new CommonSubscriber<ShareUrlBean>() {

                    @Override
                    public void onSuccess(ShareUrlBean shareUrlBean) {
                        Log.d(TAG, "success,shareUrlBean is " + shareUrlBean);
                        if (shareUrlBean == null) {
                            return;
                        }
                        if (mView != null) {
                            mView.showShareUrl(shareUrlBean.getHref());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                    }
                }));
    }

    @Override
    public void countVisitNum(GoodsSkuBean.Goods good) {
        String userId = Account.getInstance().getUserid();
        if (userId == null) {
            return;
        }
        addSubscribe(mDataManager.countVisitNum(good.getGoodsId(), good.getCateId(), userId, TimeUtil.getCurrentTime())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {

                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d(TAG, "success, countVisitNum httpResponse is " + httpResponse);

                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                    }
                }));
    }
}
